package com.sengled.cloud.biz.union.mgt.dao.sys.provider;

import com.sengled.cloud.biz.union.mgt.model.po.Role;
import com.sengled.cloud.biz.union.mgt.model.request.UserListQueryRequest;
import com.sengled.cloud.biz.union.mgt.model.vo.ShopQueryParamsBean;
import com.sengled.cloud.biz.union.mgt.model.vo.UserVo;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.SelectProvider;

import java.text.MessageFormat;
import java.util.List;

/**
 * Created by csolo on 2017/11/8.
 */
public class UserDaoProvider {

    public String queryUserSQL(UserListQueryRequest userListQueryRequest) {
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT u.id id,user_code userCode,user_name userName,r.role_name roleName,s.shop_name shopName,s.seal_mark shopDeleted,ur.seal_mark roleDeleted FROM user u,role r,user_role ur,shop s WHERE u.id=ur.user_id and r.id=ur.role_id and u.shop_id=s.id and u.int001 is null ");
        StringBuilder setStr = new StringBuilder();
   
        if(StringUtils.isNotEmpty(userListQueryRequest.getUserCode())){
            setStr.append(" and user_code like CONCAT('%', #{userCode}, '%') ");
        }
        if(StringUtils.isNotEmpty(userListQueryRequest.getUserName())){
            setStr.append(" and user_name like CONCAT('%', #{userName}, '%') ");
        }
        if(userListQueryRequest.getRoleId() != null && userListQueryRequest.getRoleId()!= 0){
            setStr.append(" and ur.role_id = #{roleId}");
        }
        if(userListQueryRequest.getShopId() != null && userListQueryRequest.getShopId()!= 0){
            setStr.append(" and shop_id = #{shopId}");
        }
        if(userListQueryRequest.getCompanyId() != null){
            setStr.append(" and u.company_id = #{companyId}");
        }
        if(!userListQueryRequest.isSuperMgr()&&userListQueryRequest.getUserId() != null){
            setStr.append(" and u.shop_id in (select distinct(shop_id) from shop_account where id in (select shop_account_id from user_acct_shop_acct where user_account_id = #{userId}))");
        }
        
        //setStr.append(" and r.enable_mark=1 and r.seal_mark = 0 and r.role_type=1");
        setStr.append(" and u.seal_mark = 0" );
        
        if(userListQueryRequest.getStart()!=null&&userListQueryRequest.getPageSize()!=null){
        	setStr.append(" order by u.create_date desc LIMIT #{start},#{pageSize}" );
        }
       
        sb.append(setStr);
        System.out.println("sql : ---------------->"+sb.toString());
        return sb.toString();
    }
    
    public String queryUserTotalRecords(UserListQueryRequest userListQueryRequest) {
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT count(u.id) FROM user u,role r,user_role ur,shop s WHERE u.id=ur.user_id and r.id=ur.role_id and u.shop_id=s.id and u.int001 is null ");
        StringBuilder setStr = new StringBuilder();
        
        if(StringUtils.isNotEmpty(userListQueryRequest.getUserCode())){
            setStr.append(" and user_code like CONCAT('%', #{userCode}, '%') ");
        }
        if(StringUtils.isNotEmpty(userListQueryRequest.getUserName())){
            setStr.append(" and user_name like CONCAT('%', #{userName}, '%') ");
        }
        if(userListQueryRequest.getRoleId() != null && userListQueryRequest.getRoleId()!= 0){
            setStr.append(" and ur.role_id = #{roleId}");
        }
        if(userListQueryRequest.getShopId() != null && userListQueryRequest.getShopId()!= 0){
            setStr.append(" and shop_id = #{shopId}");
        }
        if(userListQueryRequest.getCompanyId() != null){
            setStr.append(" and u.company_id = #{companyId}");
        }
        if(!userListQueryRequest.isSuperMgr()&&userListQueryRequest.getUserId() != null){
        	setStr.append(" and u.shop_id in (select distinct(shop_id) from shop_account where id in (select shop_account_id from user_acct_shop_acct where user_account_id = #{userId}))");
        }
       // setStr.append(" and r.enable_mark=1 and r.seal_mark = 0 and r.role_type=1");
        setStr.append(" and u.seal_mark = 0 order by u.create_date desc " );
        
        sb.append(setStr);
        System.out.println("sql : ---------------->"+sb.toString());
        return sb.toString();
    }
    

}
